﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LootPoints.Security;

namespace LootPoints.Pages
{
    public partial class Raidscore : AdminPageSecurity
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            try { if (Request.QueryString[0] == null) Response.Redirect("~/Pages/Overview.aspx"); }
            catch { Response.Redirect("~/Pages/Overview.aspx"); }

            try { if (!(bool)Session["isadmin"]) { lbtnAdmin.Visible = false; } }
            catch { lbtnAdmin.Visible = false; }

            try
            {
                if (!(bool)Session["logged"]) { lbtnLogoff.Visible = false; lbtnLogin.Visible = true; }
                else { lbtnLogoff.Visible = true; lbtnLogin.Visible = false; }
            }
            catch { lbtnLogoff.Visible = false; lbtnLogin.Visible = true; }

            Session["pload"] = Request.QueryString[0];

            Reload();
        }

        protected void lbtnLogoff_Click(object sender, EventArgs e)
        {
            Session.Clear();
            Response.Redirect("~/Default.aspx");
        }
        protected void lbtnLogin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Login.aspx");
        }
        protected void lbtnAdmin_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Adminpage.aspx");
        }
        protected void lbtnOverview_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Overview.aspx");
        }

        protected void Reload()
        {
            while (table.Rows.Count != 1) table.Rows.RemoveAt(table.Rows.Count - 1);

            string player = Session["pload"].ToString();
            lblPlayer.Text = player;

            ModelContainer context = new ModelContainer();
            RaidScore score = (from c in context.RaidScore where c.Character.Name == player select c).FirstOrDefault();
            RSReference reference = (from c in context.RSReference select c).FirstOrDefault();

            if (score == null)
            {
                score = new RaidScore();
                score.A01 = 1;
                score.A02 = 1;
                score.A03 = 1;
                score.A04 = 1;
                score.A05 = 1;
                score.A06 = 1;
                score.A07 = 1;
                score.A08 = 1;
                score.A09 = 1;
                score.A10 = 1;
                score.A11 = 1;
                score.A12 = 1;
                score.A13 = 1;
                score.A14 = 1;
                score.A15 = 1;
                score.Remark = "";
                score.Character = (from c in context.Character where c.Name == player select c).FirstOrDefault();
                context.RaidScore.Add(score);
                context.SaveChanges();
            }

            if (score != null && reference != null)
            {
                foreach (KeyValuePair<int, string[]> item in reference.List)
                {
                    TableRow row = new TableRow() { CssClass = "Row" };
                    row.Cells.Add(new TableCell() { Text = item.Value[0], HorizontalAlign = HorizontalAlign.Left });
                    row.Cells.Add(new TableCell() { Text = "" });
                    row.Cells.Add(new TableCell() { Text = item.Value[1], HorizontalAlign = HorizontalAlign.Center });

                    TableCell scorecell = new TableCell();
                    TextBox scoretb = new TextBox() { Text = score.List[item.Key].ToString(), CssClass = "tbControl", ToolTip = item.Key.ToString() };
                    scorecell.Controls.Add(scoretb);
                    row.Cells.Add(scorecell);

                    row.Cells.Add(new TableCell() { Text = "" });
                    row.Cells.Add(new TableCell() { Text = (Convert.ToInt32(row.Cells[2].Text) * Convert.ToInt32((row.Cells[3].Controls[0] as TextBox).Text)).ToString(), HorizontalAlign = HorizontalAlign.Center });
                    table.Rows.Add(row);
                }
            }
            if (reference == null) lblStatus.Text = "No Criteria found...";
            tbRemark.Text = score.Remark;
        }

        protected void btnSend_Click(object sender, EventArgs e)
        {
            string player = Session["pload"].ToString();
            ModelContainer context = new ModelContainer();
            RaidScore score = (from c in context.RaidScore where c.Character.Name == player select c).FirstOrDefault();

            score.A01 = Convert.ToInt32(((TextBox)table.Rows[1].Cells[3].Controls[0]).Text);
            score.A02 = Convert.ToInt32(((TextBox)table.Rows[2].Cells[3].Controls[0]).Text);
            score.A03 = Convert.ToInt32(((TextBox)table.Rows[3].Cells[3].Controls[0]).Text);
            score.A04 = Convert.ToInt32(((TextBox)table.Rows[4].Cells[3].Controls[0]).Text);
            score.A05 = Convert.ToInt32(((TextBox)table.Rows[5].Cells[3].Controls[0]).Text);
            score.A06 = Convert.ToInt32(((TextBox)table.Rows[6].Cells[3].Controls[0]).Text);
            score.A07 = Convert.ToInt32(((TextBox)table.Rows[7].Cells[3].Controls[0]).Text);
            score.A08 = Convert.ToInt32(((TextBox)table.Rows[8].Cells[3].Controls[0]).Text);
            score.A09 = Convert.ToInt32(((TextBox)table.Rows[9].Cells[3].Controls[0]).Text);
            score.A10 = Convert.ToInt32(((TextBox)table.Rows[10].Cells[3].Controls[0]).Text);
            score.A11 = Convert.ToInt32(((TextBox)table.Rows[11].Cells[3].Controls[0]).Text);
            score.A12 = Convert.ToInt32(((TextBox)table.Rows[12].Cells[3].Controls[0]).Text);
            score.A13 = Convert.ToInt32(((TextBox)table.Rows[13].Cells[3].Controls[0]).Text);
            score.A14 = Convert.ToInt32(((TextBox)table.Rows[14].Cells[3].Controls[0]).Text);
            score.A15 = Convert.ToInt32(((TextBox)table.Rows[15].Cells[3].Controls[0]).Text);

            score.Remark = tbRemark.Text;

            context.SaveChanges();
            Reload();
        }
    }
}